﻿using System;
using System.Text.RegularExpressions;

namespace Util.TitleParser
{
    //Format 118 & 1218
    class Parse_see : ITitleParser
    {
        public void ParseTitle(string Title, out string Name, out string Details, out uint Season, out uint Episode)
        {
            int firstLetterAdjustment;
            Match m = Regex.Match(Title, ".[0-9][0-3][0-9]");
            while(true)
            {
                if (!m.Success)
                {
                    throw new ArgumentException("Couldn't parse Title", "Title");
                }

                if (m.Value[0] != '0' && m.Value[0] != '1' && m.Value[0] != '2')
                {
                    firstLetterAdjustment = 1;
                }
                else
                {
                    firstLetterAdjustment = 0;
                }

                Season = uint.Parse(m.Value.Substring(firstLetterAdjustment, 2 - firstLetterAdjustment), System.Globalization.CultureInfo.InvariantCulture);
                Episode = uint.Parse(m.Value.Substring(2, 2), System.Globalization.CultureInfo.InvariantCulture);

                if(Episode == 20 && Season == 7) //Special cases 720 & 1080 which probably aren't season/episode numbers
                {
                    try
                    {
                        if (Title[m.Index + 4] == 'p' || Title[m.Index + 4] == 'P')
                        {
                            m = m.NextMatch();
                        }
                        else
                        {
                            break;
                        }
                    }
                    catch (Exception)
                    {
                    }
                    break;
                }
                else if(Episode == 80 && Season == 10)
                {
                    m = m.NextMatch();
                }
                else
                {
                    break;
                }
            }

            try
            {
                Name = Title.Substring(0, m.Index - 1 + firstLetterAdjustment);
            }
            catch (Exception)
            {
                Name = Title;
            }

            try
            {
                Details = Title.Substring(m.Index + 4);
            }
            catch (Exception)
            {
                Details = Title;
            }

        }
    }
}
